<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'">
          <el-form-item label="订单标题" prop="smstitle">
          <el-input v-model="dataForm.smstitle" placeholder="订单标题"></el-input>
      </el-form-item>
          <el-form-item label="订单数量" prop="quantity">
          <el-input v-model="dataForm.quantity" placeholder="订单数量"></el-input>
      </el-form-item>
          <el-form-item label="订单支付时间，10位时间戳" prop="paytime">
          <el-input v-model="dataForm.paytime" placeholder="订单支付时间，10位时间戳"></el-input>
      </el-form-item>
          <el-form-item label="" prop="total">
          <el-input v-model="dataForm.total" placeholder=""></el-input>
      </el-form-item>
          <el-form-item label="订单状态，外卖、话费、闪购、优选、酒店、美团电商、到餐、到综类订单会返回该字段

1 外卖闪购为订单已提交；其他业务为订单已付款

8 已完成

9 已退款（包括部分退款和全额退款）或风控退" prop="status">
          <el-input v-model="dataForm.status" placeholder="订单状态，外卖、话费、闪购、优选、酒店、美团电商、到餐、到综类订单会返回该字段

1 外卖闪购为订单已提交；其他业务为订单已付款

8 已完成

9 已退款（包括部分退款和全额退款）或风控退"></el-input>
      </el-form-item>
          <el-form-item label="" prop="actId">
          <el-input v-model="dataForm.actId" placeholder=""></el-input>
      </el-form-item>
          <el-form-item label="" prop="ordertime">
          <el-input v-model="dataForm.ordertime" placeholder=""></el-input>
      </el-form-item>
          <el-form-item label="订单时间支付金额" prop="payPrice">
          <el-input v-model="dataForm.payPrice" placeholder="订单时间支付金额"></el-input>
      </el-form-item>
          <el-form-item label="" prop="productId">
          <el-input v-model="dataForm.productId" placeholder=""></el-input>
      </el-form-item>
          <el-form-item label="" prop="productName">
          <el-input v-model="dataForm.productName" placeholder=""></el-input>
      </el-form-item>
          <el-form-item label="订单类型" prop="businessLine">
          <el-input v-model="dataForm.businessLine" placeholder="订单类型"></el-input>
      </el-form-item>
          <el-form-item label="子订单类型" prop="subBusinessLine">
          <el-input v-model="dataForm.subBusinessLine" placeholder="子订单类型"></el-input>
      </el-form-item>
          <el-form-item label="外卖、闪购、酒店不推送该信息，字段书写方式[X,X]，例如[2,3]，含义为点击归因，且为首单
转化类型（CPS订单：

1-券归因

2-点击归因

____________________________

CPA订单：

3-首单

4-复购

5-留存

6-二单

7-唤起）" prop="tradeTypes">
          <el-input v-model="dataForm.tradeTypes" placeholder="外卖、闪购、酒店不推送该信息，字段书写方式[X,X]，例如[2,3]，含义为点击归因，且为首单
转化类型（CPS订单：

1-券归因

2-点击归因

____________________________

CPA订单：

3-首单

4-复购

5-留存

6-二单

7-唤起）"></el-input>
      </el-form-item>
          <el-form-item label="创建时间" prop="createTime">
          <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
      </el-form-item>
          <el-form-item label="更新时间" prop="updateTime">
          <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
      </el-form-item>
          <el-form-item label="订单ID" prop="orderId">
          <el-input v-model="dataForm.orderId" placeholder="订单ID"></el-input>
      </el-form-item>
          <el-form-item label="扩展字段，原始报文" prop="extend">
          <el-input v-model="dataForm.extend" placeholder="扩展字段，原始报文"></el-input>
      </el-form-item>
          <el-form-item label="0未处理，1已处理" prop="handType">
          <el-input v-model="dataForm.handType" placeholder="0未处理，1已处理"></el-input>
      </el-form-item>
          <el-form-item label="媒体ID" prop="appkey">
          <el-input v-model="dataForm.appkey" placeholder="媒体ID"></el-input>
      </el-form-item>
          <el-form-item label="广告位ID" prop="sid">
          <el-input v-model="dataForm.sid" placeholder="广告位ID"></el-input>
      </el-form-item>
      </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        id: '',
        smstitle: '',
        quantity: '',
        paytime: '',
        total: '',
        status: '',
        actId: '',
        ordertime: '',
        payPrice: '',
        productId: '',
        productName: '',
        businessLine: '',
        subBusinessLine: '',
        tradeTypes: '',
        createTime: '',
        updateTime: '',
        orderId: '',
        extend: '',
        handType: '',
        appkey: '',
        sid: ''
      }
    }
  },
  computed: {
    dataRule () {
      return {
        smstitle: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        quantity: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        paytime: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        total: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        status: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        actId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        ordertime: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        payPrice: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        productId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        productName: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        businessLine: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        subBusinessLine: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        tradeTypes: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        createTime: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        updateTime: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        orderId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        extend: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        handType: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        appkey: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        sid: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/adx/meituanorder/${this.dataForm.id}`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {})
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.id ? 'post' : 'put']('/adx/meituanorder/', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {})
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
